import { McpServer, ResourceTemplate, type ReadResourceTemplateCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
import { Variables } from "@modelcontextprotocol/sdk/shared/uriTemplate.js";

export function registerGreetingResource(server: McpServer) {
  server.resource(
    "greeting",
    new ResourceTemplate("greeting://{name}", { list: undefined }),
    handleFunc // 引用独立函数
  );
}

// 独立处理函数
// 假设我们从 uri 中提取 name 参数
const handleFunc: ReadResourceTemplateCallback = async (uri: URL, variables: Variables, extra: RequestHandlerExtra) => {
  const url = new URL(uri.href);
  const name = variables.name;
  return {
    contents: [{
      uri: uri.href,
      text: `Hello, ${name || 'Guest'}!`
    }]
  };
}